package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Category;

/**
 * Created by Max on 30.03.2014.
 */
public class MenuCategoryContainer extends FlowPanel implements
    HasValueChangeHandlers<JSONArray>, HasOpenHandlers<Category>, HasSelectionHandlers<MenuCategoryBox> {

    private PickupDragController widgetDragController;

    public MenuCategoryContainer(AbsolutePanel dragSpace) {
        dragSpace.add(this);
        this.addStyleName("content-container");
        // initialize a widget drag controller
        this.widgetDragController = new PickupDragController(dragSpace, false);
        this.widgetDragController.setBehaviorMultipleSelection(false);
        // initialize a widget drop controller
        MenuCategoryDropController menuCategoryDropController = new MenuCategoryDropController(this);
        // register drop controller
        this.widgetDragController.registerDropController(menuCategoryDropController);
        this.widgetDragController.setBehaviorDragStartSensitivity(3);
    }

    @Override
    public HandlerRegistration addOpenHandler(OpenHandler<Category> handler) {
        return super.addHandler(handler, OpenEvent.getType());
    }
    @Override
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<JSONArray> handler) {
        return super.addHandler(handler, ValueChangeEvent.getType());
    }
    @Override
    public HandlerRegistration addSelectionHandler(SelectionHandler<MenuCategoryBox> handler) {
        return super.addHandler(handler, SelectionEvent.getType());
    }

    private void addBox(final MenuCategoryBox box) {
        this.add(box);
        box.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                MenuCategoryContainer.this.setBoxSelected(box);
            }
        });
        box.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent event) {
                OpenEvent.fire(MenuCategoryContainer.this,box.getCategory());
            }
        });
        // make the widget draggable
        this.widgetDragController.makeDraggable(box);
    }

    protected void setBoxSelected(MenuCategoryBox newSelectedBox) {
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuCategoryBox) {
                MenuCategoryBox mcBox = (MenuCategoryBox)box;
                if (mcBox.isSelected()) {
                    mcBox.setSelected(false);
                }
            }
        }
        newSelectedBox.setSelected(true);
        SelectionEvent.fire(this,newSelectedBox);
    }

    public void setData(JSONArray data) {
        this.clear();
        int n = data.size();
        if (n > 0) {
            for(int i = 0; i < n; i++) {
                JSONObject obj = data.get(i).isObject();
                final Category category = new Category(obj);
                addBox(new MenuCategoryBox (category));
            }
        }
    }

    private JSONArray getData() {
        JSONArray data = new JSONArray();
        int i = 0;
        for (Widget box : this.getChildren()) {
            if (box instanceof MenuCategoryBox) {
                MenuCategoryBox mcBox = (MenuCategoryBox) box;
                mcBox.setCategoryOrder(this.getWidgetIndex(box));
                data.set(i++, mcBox.getCategory().getJSON());
            }
        }
        return data;
    }

    protected void orderChangeEventFire() {
        JSONArray data = this.getData();
        ValueChangeEvent.fire(this, data);
    }



}
